#region Imports

using System;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

#endregion

namespace IEC.ColumnOrdering.SP2007.Application.Services.Concrete
{

    /// <summary>
    /// Provides navigation services for SharePoint 2007.
    /// </summary>
    internal sealed class PlatformNavigationService : NavigationService
    {

        #region Constants

        /// <summary>
        /// Represents the key of the Source URL, in the request.
        /// </summary>
        private const string SourceUrlRequestKey = @"s";

        /// <summary>
        /// Represents the HTTP_REFERER Server Variable key.
        /// </summary>
        private const string HttpReferrerServerKey = @"HTTP_REFERER";

        #endregion

        #region Properties - Static Member

        /// <summary>
        /// Gets the current web URL.
        /// </summary>
        /// <value>
        /// The current web URL.
        /// </value>
        private static Uri CurrentWebUrl
        {
            get
            {
                string textUrl = SPContext.Current.Web.Url;

                return new Uri(textUrl);
            }
        }

        /// <summary>
        /// Gets the source URL.
        /// </summary>
        /// <value>
        /// The source URL.
        /// </value>
        private static Uri SourceUrl
        {
            get
            {
                string textUrl = HttpContext.Current.Request[
                    PlatformNavigationService.SourceUrlRequestKey];

                return new Uri(textUrl);
            }
        }

        /// <summary>
        /// Gets the referrer URL.
        /// </summary>
        /// <value>
        /// The referrer URL.
        /// </value>
        private static Uri ReferrerUrl
        {
            get
            {
                string textUrl = HttpContext.Current.Request.ServerVariables[
                    PlatformNavigationService.HttpReferrerServerKey];

                return new Uri(textUrl);
            }
        }

        #endregion

        #region Methods - Static Member

        /// <summary>
        /// Transitions the view to the specified URL.
        /// </summary>
        /// <param name="url">
        /// The URL to which the view must be transitioned.
        /// </param>
        private static void TransitionView(Uri url)
        {
            // redirect to requesting view
            HttpContext.Current.Response.Redirect(url.ToString());
        }

        #endregion

        #region Methods - Instance Member

        #region Methods - Instance Member - INavigationService Members

        /// <summary>
        /// Navigates to the specified view.
        /// </summary>
        /// <param name="view">
        /// The view to which the user is to be navigated.
        /// </param>
        public override void NavigateToView(Uri view)
        {
            PlatformNavigationService.TransitionView(view);
        }

        /// <summary>
        /// Navigates to the specified view.
        /// </summary>
        /// <param name="view">
        /// The view to which the user is to be navigated.
        /// </param>
        public override void NavigateToView(string view)
        {
            Uri url = new Uri(view);

            PlatformNavigationService.TransitionView(url);
        }

        /// <summary>
        /// Navigates to the requesting view.
        /// </summary>
        public override void NavigateToRequestingView()
        {
            Uri url = PlatformNavigationService.CurrentWebUrl;

            if (PlatformNavigationService.SourceUrl != null)
            {
                url = PlatformNavigationService.SourceUrl;
            }
            else if (PlatformNavigationService.ReferrerUrl != null)
            {
                url = PlatformNavigationService.ReferrerUrl;
            }

            if (url != null)
            {
                this.NavigateToView(url);
            }
        }

        /// <summary>
        /// Navigates to the error view, displaying the specified exception.
        /// </summary>
        /// <param name="exception">
        /// The exception to be displayed on the error page.
        /// </param>
        public override void NavigateToErrorView(Exception exception)
        {
            this.NavigateToErrorView(exception.ToString());
        }

        /// <summary>
        /// Navigates to the error view, displaying the specified message.
        /// </summary>
        /// <param name="message">
        /// The message to be displayed on the error page.
        /// </param>
        public override void NavigateToErrorView(string message)
        {
            SPUtility.TransferToErrorPage(message);
        }

        /// <summary>
        /// Gets the source URL.
        /// </summary>
        /// <returns>
        /// The source URL
        /// </returns>
        public override Uri GetSourceUrl()
        {
            return PlatformNavigationService.SourceUrl;
        }
        
        #endregion

        #endregion

    }

}
